package com.xncoding.Queue;

import java.util.concurrent.ArrayBlockingQueue;

public class BlockingDemo01 {
    static ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(5);

    public static void main(String[] args) throws Exception {
        new Thread("生产者") {
            public void run() {
                while (true) {
                    try {
                        queue.put(1);
                        System.out.println("队列中添加了一个元素, size=" + queue.size());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();

        Thread.sleep(500);

        new Thread("消费者") {
            public void run() {
                while (true) {
                    try {
                        queue.take();
                        System.out.println("队列中删除了一个元素, size=" + queue.size());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }.start();
    }
}
